/*=============================================================================

  APPLICATION MANAGER IO DEVICE INTERFACE MODULE

  FILE: SYSmgr_HIO.cpp
  
  DESCRIPTION

    Appliacation Manager IO Interface Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================
	                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   IO Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "SYSmgr.h"
#include "HIO_cfg.h"

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/
#include <devSP/ioHal_emul.h>
//#include <devSP/ioHal_AXT.h>

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED VARIABLE DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
static dword ioDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser);

/*=============================================================================
  CLASS IMPLEMENTATION
=============================================================================*/
void SYSmgr::HIO_Alloc(void)
{
	ObjClient_s		c;
	
	m_pIO = new HIO;
	
	m_ioInit = FALSE;
	m_ioCFG = NULL;

	c.pUserCxt = this;
	c.EventCbf = ioDevEventCallback;
	m_pIO->SetClient(&c);

#ifdef TG_HW_IODEV_SIMUL
	m_pIO->HAL_Register(DIODEV_EMULATOR, DioHal_Emul_Register, DioHal_Emul_Deregister);
#else
	//m_pIO->HAL_Register(DIODEV_AXT_SIO, DioHal_AXTSIO_Register, DioHal_AXTSIO_Deregister);
#endif
}

void SYSmgr::HIO_Free(void)
{
	if(m_pIO == NULL)
		return;

	m_pIO->Close();
	if(m_ioCFG)
	{
		memFree(m_ioCFG);
		m_ioCFG = NULL;
	}
	// Deregister device support package
#ifdef TG_HW_IODEV_SIMUL
	m_pIO->HAL_Deregister(DIODEV_EMULATOR);
#else
	//m_pIO->HAL_Deregister(DIODEV_AXT_SIO);
#endif

	if(m_pIO)
	{
		HIO_Close();

		delete m_pIO;
		m_pIO = NULL;
	}
}

boolean SYSmgr::HIO_Setup(char *pszFile)
{
	if(m_ioCFG)
	{
		memFree(m_ioCFG);
		m_ioCFG = NULL;
	}

	if(pszFile != NULL)
	{
		m_ioCFG = (char*)memCalloc(strlen(pszFile) + 1);
		strcpy(m_ioCFG, pszFile);
	}

	return	TRUE;
}

boolean SYSmgr::HIO_Init(void)
{
	iodev_cfg_s		*cfg;

	Event_Handler(HIO_DEV, INIT_START_E, 0, 0);

	if(m_ioInit == TRUE)
	{
		Event_Handler(HIO_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	// Check the configuration file
	cfg = iodev_cfg_load(m_ioCFG);
	
	if(cfg == NULL)
	{
		LOGWARN("[ioDev] <%s> loading failure.", m_ioCFG);
		LOGWARN("[ioDev] Load the default configuration.");

		cfg = iodev_default_alloc();
		
		if(iodev_cfg_save(cfg, m_ioCFG) == FALSE)
		{
			iodev_default_free(cfg);
			LOGERR("[ioDev] Default configuration save failure.");

			return	FALSE;
		}
		iodev_default_free(cfg);
	}
	else
	{
		iodev_cfg_free(cfg);
	}

	if(m_pIO->Init(m_ioCFG) == TRUE)
	{
		m_pIO->INIT_IO_STATE();
		
		m_ioInit = TRUE;
		Event_Handler(HIO_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}
	Event_Handler(HIO_DEV, ERR_INIT_E, HDEV_MAX, 0);

	return	FALSE;
}

boolean SYSmgr::HIO_Close(void)
{
	Event_Handler(HIO_DEV, CLOSE_START_E, 0, 0);

	if(m_ioInit == FALSE)
	{
		Event_Handler(HIO_DEV, CLOSE_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}
	
	m_pIO->INIT_IO_STATE();
	if(m_pIO->Close() == TRUE)
	{
		m_ioInit = FALSE;
		Event_Handler(HIO_DEV, CLOSE_DONE_E, 0, 0);
		return	TRUE;
	}
	m_ioInit = FALSE;
	Event_Handler(HIO_DEV, ERR_CLOSE_E, 0, 0);

	return	TRUE;
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
dword ioDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser)
{
	SYSmgr		*pCxt = (SYSmgr*)pUser;

	if(pCxt)
	{
		return	pCxt->HEvent_Handler(HIO_DEV, dwEvent, wParam, lParam);
	}
	return	(0);
}

/*--- END OF SYSmgr_HIO.CPP ---*/
